package Hander.Updata;

import View.Add.AddAchievementView;
import View.LeaderView;
import View.Updata.UpdataAchievementView;
import entity.Achievement;
import service.PersonnelService;
import service.PersonnelServiceImpl;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UpdataAchievementViewHander implements ActionListener {
    private UpdataAchievementView updataAchievementView;
    private LeaderView leaderView;
    public UpdataAchievementViewHander(UpdataAchievementView updataAchievementView, LeaderView leaderView) {
        this.updataAchievementView = updataAchievementView;
        this.leaderView=leaderView;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton=(JButton) e.getSource();
        String text=jButton.getText();
        if("修改".equals(text)){
            PersonnelService personnelService=new PersonnelServiceImpl();
            Achievement achievement= updataAchievementView.buildAchievement();
            int key=Integer.parseInt(leaderView.getSelectedStudentIds()[0]);
            Boolean addresult=personnelService.updata(key,achievement.getAllattributes(), updataAchievementView.getTablesName());
            if(addresult){
                leaderView.reloadTable(updataAchievementView.getTablesName());
                JOptionPane.showMessageDialog(updataAchievementView,"修改成功");
                updataAchievementView.dispose();
            }else if(achievement.getGrade()>4||achievement.getGrade()<0){
                JOptionPane.showMessageDialog(updataAchievementView,"Grade取值异常！");
            }
            else{
                System.out.println("修改错误！");
                JOptionPane.showMessageDialog(updataAchievementView,"修改失败");
            }
        }
    }
}
